#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import time
import os
import random
from urllib import parse
import logging

from pjdog_public import load_record, write_record
from pjdog_func import get_session, download as down_load, format_element


class PJDogDeal(object):
    def __init__(self, config):
        self.config = config
        logging.info('config {0}'.format(config))

    # 处理线程 读取任务
    def run(self):
        deals = load_record(self.config.record_file)
        filters = load_record(self.config.filter_file)
        session = get_session(self.config.login_url, self.config.post_data, self.config.proxy)
        with open(self.config.task_file, encoding='utf-8') as rfp:
            for id_str in rfp:
                task_id = format_element(id_str)
                if task_id in deals or task_id in filters:
                    continue
                url = '{0}{1}'.format(self.config.request_url, task_id)
                file_name = '{0}{1}.html'.format(self.config.data_path, task_id)
                if os.path.exists(file_name):
                    size = os.stat(file_name).st_size
                    if size >= self.config.min_size:
                        logging.warning('file {0} exists, url {1}'.format(file_name, url))
                        continue
                logging.info('{0} {1}'.format(url,file_name))
                # time.sleep(random.randint(1, 2))
                status = down_load(session, url, file_name)
                if status != 200:
                    session = get_session(self.config.login_url, self.config.post_data, self.config.proxy)
                else:
                    write_record(self.config.record_file, task_id)
